package com.xuedongzhilian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuedongzhilian.dto.ExamQuestionDTO;
import com.xuedongzhilian.entity.ExamQuestion;
import com.xuedongzhilian.mapper.ExamQuestionMapper;
import com.xuedongzhilian.service.IExamQuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 试卷题目服务实现类
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Service
public class ExamQuestionServiceImpl implements IExamQuestionService {

    @Resource
    private ExamQuestionMapper examQuestionMapper;

    @Override
    public boolean createExamQuestion(ExamQuestionDTO examQuestionDTO) {
        ExamQuestion examQuestion = BeanUtil.copyProperties(examQuestionDTO, ExamQuestion.class);
        return examQuestionMapper.insert(examQuestion) > 0;
    }

    @Override
    public List<ExamQuestionDTO> getExamQuestionsByPaperId(Long paperId) {
        List<ExamQuestion> examQuestions = examQuestionMapper.selectByPaperId(paperId);
        
        // 手动转换，确保所有字段都被正确复制
        return examQuestions.stream()
                .map(examQuestion -> {
                    ExamQuestionDTO dto = new ExamQuestionDTO();
                    dto.setId(examQuestion.getId());
                    dto.setPaperId(examQuestion.getPaperId());
                    dto.setQuestionNumber(examQuestion.getQuestionNumber());
                    dto.setQuestionType(examQuestion.getQuestionType());
                    dto.setContent(examQuestion.getContent());
                    dto.setOptionA(examQuestion.getOptionA());
                    dto.setOptionB(examQuestion.getOptionB());
                    dto.setOptionC(examQuestion.getOptionC());
                    dto.setOptionD(examQuestion.getOptionD());
                    dto.setOptionE(examQuestion.getOptionE());
                    dto.setOptionF(examQuestion.getOptionF());
                    dto.setCorrectAnswer(examQuestion.getCorrectAnswer());
                    dto.setExplanation(examQuestion.getExplanation());
                    return dto;
                })
                .collect(Collectors.toList());
    }

    @Override
    public boolean updateExamQuestion(ExamQuestionDTO examQuestionDTO) {
        ExamQuestion examQuestion = BeanUtil.copyProperties(examQuestionDTO, ExamQuestion.class);
        return examQuestionMapper.updateById(examQuestion) > 0;
    }

    @Override
    public boolean deleteExamQuestion(Long questionId) {
        return examQuestionMapper.deleteById(questionId) > 0;
    }
}